//
//  ATSetPasswordViewController.m
//  Adorable_Things
//
//  Created by ybq on 16/1/19.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ATSetPasswordViewController.h"
#import "BQNavViewCommon.h"
#import "ATLoginViewController.h"

@interface ATSetPasswordViewController () <MBProgressHUDDelegate>
{
    MBProgressHUD *HUD;
}
@property (nonatomic, strong) BQNavViewCommon *navView;

@end

@implementation ATSetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
}

- (void)createUI {
    _navView = [[BQNavViewCommon alloc] initWithFrame:CGRectMake(0, 20, kWidth, 44)];
    [_navView.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _navView.titleLabel.text = _myTitle;
    [self.view addSubview:_navView];
    
    _backGroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h"]];
    _doneButton.layer.cornerRadius = 25;
    _setSecurityButton.layer.cornerRadius = 5;
}

- (void)backButtonAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)setSecurityButtonAction:(id)sender {
    if (_passwordTF.isSecureTextEntry) {
        [_setSecurityButton setImage:[UIImage imageNamed:@"passwordfalse"] forState:(UIControlStateNormal)];
        [_passwordTF setSecureTextEntry:NO];
    } else {
        [_setSecurityButton setImage:[UIImage imageNamed:@"passwordTrue"] forState:(UIControlStateNormal)];
        [_passwordTF setSecureTextEntry:YES];
    }
}
- (IBAction)doneButtonAction:(id)sender {
    
    if ([_myTitle isEqualToString:@"设置密码"]) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"nickname"] = _phone;
        parameters[@"password"] = _passwordTF.text;
        parameters[@"phone"] = _phone;
        parameters[@"verify_code"] = _verify_code;
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:@"http://api.menghuoapp.com/v1/user/register" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *result = responseObject[@"msg"];
            if ([result isEqualToString:@"OK"]) {
                
                [_passwordTF resignFirstResponder];
                [self showHUDWithString:@"恭喜您注册成功"];
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ATLoginViewController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        [controller dismissViewControllerAnimated:YES completion:nil];
                    }
                }
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    } else {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"password"] = _passwordTF.text;
        parameters[@"phone"] = _phone;
        parameters[@"verify_code"] = _verify_code;
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:@"http://api.menghuoapp.com/v1/user/reset_pwd" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *result = responseObject[@"msg"];
            if ([result isEqualToString:@"OK"]) {
                [_passwordTF resignFirstResponder];
                [self showHUDWithString:@"修改密码成功"];
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ATLoginViewController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        [controller dismissViewControllerAnimated:YES completion:nil];
                    }
                }
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }

}

#pragma mark --- HUD

- (void)showHUDWithString:(NSString *)string {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = string;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的偏移量，如果不指定则在屏幕中间显示
    //    HUD.yOffset = 150.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(3);
    } completionBlock:^{
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
